import Vue from 'vue'
import app from './module/app'
import home from './module/home'

const store = new Vue({
    data: function () {
        return {
            app,
            home
        }
    },
    methods: {
        commit (key, value) {
            const keys = key.split('/')
            let obj = null
            if (keys.length <= 1 || keys.length > 2) {
                new Error('无效')
            } else if (keys.length === 2) {
                obj = this[keys[0]].mutations[keys[1]]
                if (!obj) {
                    new Error('非法的mutations')
                }
                obj(this[keys[0]].state, value)
            }
        },
        dispatch (key, value) {
            const keys = key.split('/')
            let obj = null
            if (keys.length <= 1 || keys.length > 2) {
                new Error('无效')
            } else if (keys.length === 2) {
                obj = this[keys[0]].actions[keys[1]]
                if (!obj) {
                    new Error('非法的actions')
                }
                obj.call(this, this, value)
            }
        }
    }
})

Vue.prototype.$store = store

export default store